/*
============================================================================
 Name        : $(classname)Container.cpp
 Author	     : $(author)
 Copyright   : $(copyright)
 Description : Container control implementation
============================================================================
*/

// INCLUDE FILES
#include "$(classname)Container.h"

#include <eikclb.h>
#include <eikclbd.h>
#include <akniconarray.h>

#include "$(projectname).rsg"

#if defined(USE_BACKGOUND)
	#include <AknsDrawUtils.h>  //background
	#include <AknsBasicBackgroundControlContext.h>     //background
#endif	// USE_BACKGOUND

// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// C$(classname)Container::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void C$(classname)Container::ConstructL(const TRect& aRect)
	{
	CreateWindowL();

	CreateListBoxL();

    SetRect(aRect);
#if defined(USE_BACKGOUND)
	iBackGround = CAknsBasicBackgroundControlContext::NewL(KAknsIIDQsnBgAreaMain,Rect(),EFalse);
#endif // USE_BACKGOUND
	ActivateL();
	}

// Destructor
C$(classname)Container::~C$(classname)Container()
	{
	delete iListBox;
#if defined(USE_BACKGOUND)
	delete iBackGround;
#endif // USE_BACKGOUND
	}

// ---------------------------------------------------------
// C$(classname)Container::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void C$(classname)Container::SizeChanged()
	{
    TRect rect = Rect();
    iListBox->SetExtent(TPoint(0,0),rect.Size());
	}

// ---------------------------------------------------------
// C$(classname)Container::CountComponentControls() const
// ---------------------------------------------------------
//
TInt C$(classname)Container::CountComponentControls() const
	{
	return 1; // return nbr of controls inside this container
	}

// ---------------------------------------------------------
// C$(classname)Container::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* C$(classname)Container::ComponentControl(TInt aIndex) const
	{
	switch ( aIndex )
		{
		case 0:
			return iListBox;
		default:
			return NULL;
		}
	}

// ---------------------------------------------------------
// C$(classname)Container::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void C$(classname)Container::Draw(const TRect& aRect) const
	{
    CWindowGc& gc = SystemGc();
#if defined(USE_BACKGOUND)
	MAknsSkinInstance* skin=AknsUtils::SkinInstance();
	MAknsControlContext* cc=AknsDrawUtils::ControlContext(this);
	AknsDrawUtils::Background(skin,cc,this,gc,aRect);    
#endif // USE_BACKGOUND
	}

// ---------------------------------------------------------
// C$(classname)Container::HandleControlEventL(
//	 CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void C$(classname)Container::HandleControlEventL(
	CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
	{
	// TODO: Add your control event handler code here
	}

TKeyResponse C$(classname)Container::OfferKeyEventL(
		const TKeyEvent& aKeyEvent,	TEventCode aType)
	{
    // See if we have a selection
    TInt code = aKeyEvent.iCode;
    switch(code)
        {
		// is navigator button pressed
    	case EKeyOK:
            return (EKeyWasConsumed);
            break;
    	case EKeyLeftArrow:
    	case EKeyRightArrow:
	        return (EKeyWasNotConsumed);
	        break;

        default:
            // Let Listbox take care of its key handling
            return iListBox->OfferKeyEventL(aKeyEvent, aType);
            break;
        }	
	}
#if defined(USE_BACKGOUND)
TTypeUid::Ptr C$(classname)Container::MopSupplyObject(TTypeUid aId)
	{
	if(aId.iUid==MAknsControlContext::ETypeId&&iBackGround)
		return(MAknsControlContext::SupplyMopObject(aId,iBackGround));

	return(CCoeControl::MopSupplyObject(aId));
	}
#endif // USE_BACKGOUND

void C$(classname)Container::CreateListBoxL()
	{
    // Listbox
    iListBox = new (ELeave) CAknSingleGraphicStyleListBox ;

	iListBox->SetContainerWindowL( *this );
	iListBox->ConstructL( this, EAknListBoxMarkableList);

	// Create the scroll indicator
	iListBox->CreateScrollBarFrameL(ETrue);
	iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);

//	SetListBoxIconArrayL(R_$(projectName$upper)_$(classname$upper)_ICONS); /*icon array resouce id*/
	
    iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray );
    iListBox->ActivateL();
	}

void C$(classname)Container::SetListBoxIconArrayL(TInt aIconArrayResourceId)
	{
	// Creates a GUI icon array.
	CAknIconArray* icons =new(ELeave) CAknIconArray(EIconTotal);
	CleanupStack::PushL(icons);
	icons->ConstructFromResourceL(aIconArrayResourceId);
	// Sets graphics as ListBox icon.
	iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
	CleanupStack::Pop(); // icons
	}
